<!--应用的主布局组件，包含：
顶部导航栏（显示用户名、头像和退出登录功能）
侧边栏导航菜单（根据用户角色显示不同菜单项）
主内容区域（用于渲染路由组件）
-->
<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <el-header class="app-header">
      <div class="logo">Vue Login System</div>

      <div class="user-info">
        <span class="username">{{ userStore.user?.username }}</span>
        <el-dropdown @command="handleDropdownCommand">
          <el-button type="text" class="avatar-button">
            <el-avatar :icon="User" class="avatar"></el-avatar>
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">
                <el-icon>
                  <User/>
                </el-icon>
                <span>个人中心</span>
              </el-dropdown-item>
              <el-dropdown-item command="logout">
                <el-icon>
                  <Logout/>
                </el-icon>
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <div class="app-main">
      <!-- 侧边栏导航 -->
      <el-aside class="app-sidebar">
        <el-menu
            default-active="Home"
            class="sidebar-menu"
            router
            :unique-opened="true"
        >
          <el-menu-item index="Home">
            <el-icon>
              <Home/>
            </el-icon>
            <span>首页</span>
          </el-menu-item>

          <el-menu-item index="Profile">
            <el-icon>
              <User/>
            </el-icon>
            <span>个人中心</span>
          </el-menu-item>

          <el-menu-item index="About">
            <el-icon>
              <InfoFilled/>
            </el-icon>
            <span>关于</span>
          </el-menu-item>

          <!-- 仅管理员可见 -->
          <el-menu-item
              index="Admin"
              v-if="userStore.isAdmin"
          >
            <el-icon>
              <Setting/>
            </el-icon>
            <span>管理员页面</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主内容区域 -->
      <el-main class="app-content">
        <router-view/>
      </el-main>
    </div>
  </div>
</template>

<script setup lang="ts">
import {useRouter} from 'vue-router'
import {useUserStore} from '../stores/user'
import {ElMessage} from 'element-plus'
import {
  User,
  SwitchButton as Logout,
  Home as HomeIcon,
  InfoFilled,
  Setting
} from '@element-plus/icons-vue'


// 用户状态
const userStore = useUserStore()
const router = useRouter()

// 处理下拉菜单命令
const handleDropdownCommand = (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 退出登录
const handleLogout = () => {
  userStore.logout()
  ElMessage.success('已退出登录')
  router.push('/login')
}
</script>

<style scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background-color: #165DFF;
  color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 10;
}

.logo {
  font-size: 20px;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.username {
  font-size: 14px;
}

.avatar-button {
  padding: 0;
  color: white;
}

.avatar {
  width: 36px;
  height: 36px;
}

.app-main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.app-sidebar {
  width: 200px;
  background-color: #0F172A;
  color: white;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.sidebar-menu {
  border-right: none;
  background-color: #0F172A;
  color: white;
}

.sidebar-menu .el-menu-item {
  color: rgba(255, 255, 255, 0.7);
  background-color: #0F172A;
}

.sidebar-menu .el-menu-item.is-active {
  color: white;
  background-color: #165DFF;
}

.sidebar-menu .el-menu-item:hover {
  background-color: #1E293B;
}

.app-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background-color: #F8FAFC;
}
</style>
